<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #container {
            width: 100%;
            height: 110vh;
            background-color: white;
        }

        .custom-content-marker {
            position: relative;
            width: 25px;
            height: 34px;
        }

        .custom-content-marker img {
            width: 100%;
            height: 100%;
        }

        .custom-content-marker .close-btn {
            position: absolute;
            top: -6px;
            right: -8px;
            width: 15px;
            height: 15px;
            font-size: 12px;
            background: #ccc;
            border-radius: 50%;
            color: #fff;
            text-align: center;
            line-height: 15px;
            box-shadow: -1px 1px 1px rgba(10, 10, 10, .2);
        }

        .custom-content-marker .close-btn:hover {
            background: #666;
        }
    </style>
</head>

<body>
<div id="container"></div>
</body>

</html>

<script type="text/javascript">
    window._AMapSecurityConfig = {
        securityJsCode: "07eae51c400fb9f5b00d7cd71de9a554",
    };
</script>

<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=54f50487330ef9fc6439c8a6231a36de"></script>
<script type="text/javascript">
    //地图初始化应该在地图容器 <div> 已经添加到 DOM 树之后
    var map = new AMap.Map("container", {
        zoom: 12,
        center: [120.601254, 31.305680]
    });

    window.onload = function () {
        // 调用鸿蒙端的setCurrentLocationNear方法
        window.JiKe.setCurrentLocationNear()
    }

    //标记中心点和最近的4个点，
    function setLocationNear(lon, lat) {
        map.setZoomAndCenter(18, [lon, lat]);

        //点标记显示内容
        const markerContent = `<div class="custom-content-marker"><img src="http://a.amap.com/jsapi_demos/static/demo-center/icons/dir-via-marker.png"></div>`
        const position = new AMap.LngLat(lon, lat); //Marker 经纬度
        const marker = new AMap.Marker({
            position: position,
            content: markerContent, //将 html 传给 content
            offset: new AMap.Pixel(-13, -30), //以 icon 的 [center bottom] 为原点
        });
        map.add(marker);

        var text = new AMap.Text({
            text: "黄台隧道", //标记显示的文本内容
            anchor: "center", //设置文本标记锚点位置
            offset: [0, -50],
            style: {
                //设置文本样式，Object 同 css 样式表
                "padding": ".75rem 1.25rem",
                "margin-bottom": "1rem",
                "border-radius": ".25rem",
                "background-color": "black",
                "width": "6rem",
                "border-width": 0,
                "box-shadow": "0 2px 6px 0 rgba(114, 124, 245, .5)",
                "text-align": "center",
                "font-size": "20px",
                "color": "white",
            },
            position: new AMap.LngLat(lon, lat), //点标记在地图上显示的位置
        });
        text.setMap(map); //将文本标记设置到地图上

        //根据当前位置获取最近的4个地点并回传
        map.plugin(['AMap.PlaceSearch'], function () {
            var PlaceSearchOptions = { //设置PlaceSearch属性
                map: map,
                pageSize: 4,
                pageIndex: 1, //请求页码，默认1
                extensions: "base" //返回信息详略，默认为base（基本信息）
            };
            var MSearch = new AMap.PlaceSearch(PlaceSearchOptions); //构造PlaceSearch类
            AMap.Event.addListener(MSearch, "complete", (result) => {
                //返回结果
                window.JiKe.receiveData(JSON.stringify(result?.poiList?.pois || []))
            });
            MSearch.searchNearBy('美食', position); //关键字查询
        });


    }



</script>